<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/3/23
 * Time: 11:02
 */
namespace app\admin\controller;
use think\Db;
class GroupsController extends CommonController
{
    /**
     *getUserData 页面加载获取群组表数据
     * @return json格式数组
     */
    public function getData(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_group')->alias('g')
            ->join('ew_u_user u','g.g_user_id=u.user_id','LEFT')
            ->where($where)
            ->field('g_id,g_name,g_uids,g_thumb,g_status,g_time,user_id,user_username,user_phone')
            ->order('g_time DESC')
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total =Db::table('ew_group')->alias('g')
            ->join('ew_u_user u','g.g_user_id=u.user_id','LEFT')
            ->where($where)
            ->field('user_id')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    /**
     * 解散群
     * cid
     * @return string
     */
    public function delete(){
        $user_id = $_POST['uid'];
        $groupid = $_POST['gid'];
        $res = $this->kill($user_id,$groupid);
        if ($res['code'] != 200) {
            return json_encode(['status'=>0,'msg'=>'解散失败']);
        }else{
            Db::table('ew_group')->where('g_id', $groupid)->update(['g_status'=>2]);
            return json_encode(['status'=>200,'msg'=>'解散成功']);
//            $res = Db::table('ew_group')->where('g_id', $groupid)->delete();
//            if($res){
//                return json_encode(['status'=>200,'msg'=>'解散成功']);
//            }else{
//                return json_encode(['status'=>0,'msg'=>'解散失败']);
//            }
        }
    }
    /**
     * 解散群
     * cid
     * @return string
     */
    public function deletegroup(){
        $user_id = $_POST['uid'];
        $groupid = $_POST['gid'];
        $status = $_POST['status'];
        if($status == 1){
            $res = $this->kill($user_id,$groupid);
            if ($res['code'] != 200) {
                return json_encode(['status'=>0,'msg'=>'删除之前解散失败']);
            }
        }
        $res = Db::table('ew_group')->where('g_id', $groupid)->delete();
        if($res){
            return json_encode(['status'=>200,'msg'=>'删除成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'删除失败']);
        }
    }

    /**
     * seeUserInfo 群组详情
     * @return 返回状态
     */
    public function seeInfo(){
        $gid = input('gid');
        $data = Db::table('ew_group')->alias('g')
            ->join('ew_u_user u','g.g_user_id=u.user_id','LEFT')
            ->where('g_id',$gid)
            ->field('g.*,user_id,user_username,user_phone')
            ->find();
        if($data['g_uids']){
            $data['g_uids'] = trim($data['g_uids'],',');
            $data['g_uids'] = Db::table('ew_u_user')->where('user_id','in',"{$data['g_uids']}")
            ->field('user_id,user_username,user_phone,user_image')
                ->select();
        }else{
            $data['g_uids'] = array();
        }
        $this->assign ('data',$data);
        return $this->fetch("groups/group_info");
    }


}